package com.item.audrey_hepburn.controller.db2;

import com.item.audrey_hepburn.entity.db2.ResultMap;
import com.item.audrey_hepburn.entity.db2.User;
import com.item.audrey_hepburn.mapper.db2.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @Autowired
    private UserMapper userMapper;


    private ResultMap resultMap= ResultMap.getResultMap();


    @RequestMapping(value = "/notLogin", method = RequestMethod.GET)
    public ModelAndView notLogin(ModelAndView mv) {

        resultMap.success();
        resultMap.setMessage("您尚未登陆");
        mv.addObject("result",resultMap);
        mv.setViewName("login");
        return mv;
       // return "login";

    }

    @RequestMapping(value = "/notRole", method = RequestMethod.GET)
    public ResultMap notRole() {
        resultMap.success();
        resultMap.setMessage("您没有权限");
        return resultMap;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ResultMap logout() {
        Subject subject = SecurityUtils.getSubject();
        //注销
        subject.logout();
        resultMap.success();
        resultMap.setMessage("成功注销");
        return resultMap;
    }

    /**
     * 登陆
     *
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password) {
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行认证登陆
        subject.login(token);

       User user = userMapper.findByName(username);
        System.out.println(user.getPassword());
       if(null!=user){
           return "/index/index";
       }else {
           return "login";
       }



        //根据权限，指定返回数据
       /* String role = userMapper.findByName(username).getRole();


        if ("user".equals(role)) {
            resultMap.success();
            resultMap.setMessage("欢迎登陆");
            return resultMap;

        }
        if ("admin".equals(role)) {
            resultMap.success();
            resultMap.setMessage("欢迎登陆");
            return resultMap;

        }
        resultMap.success();
        resultMap.setMessage("权限错误");
        return resultMap;*/
        //return resultMap.fail().message("权限错误！");
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String inedx(ModelAndView mv) {
        resultMap.success();
        resultMap.setMessage("您尚未登陆");
        System.out.println("index来了");
        mv.addObject("result",resultMap);
        return "/index/index_v1";

    }

    @RequestMapping(value = "/index2", method = RequestMethod.GET)
    public String inedx2(ModelAndView mv) {
        resultMap.success();
        resultMap.setMessage("您尚未登陆");
        System.out.println("index来了");
        mv.addObject("result",resultMap);
        return "/index/index_v2";

    }
}